package com.blb.community.controller.repair;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.community.mapper.ZyCommunityMapper;
import com.blb.community.pojo.ResponseResult;
import com.blb.community.pojo.VO.RepairUpdate;
import com.blb.community.pojo.ZyCommunity;
import com.blb.community.pojo.ZyRepairNew;
import com.blb.community.pojo.zy.ZyRepair;
import com.blb.community.service.RepairService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class RepairController {
    @Resource
    private RepairService repairService;

    @Resource
    private ZyCommunityMapper zyCommunityMapper;


    @GetMapping("/repair-community")
    public ResponseResult getCommunity(){
        List<ZyCommunity> zyCommunities = zyCommunityMapper.selectList(null);
        return ResponseResult.ok(zyCommunities);
    }

    @PutMapping("/repair")
    public ResponseResult update(@RequestBody RepairUpdate repairUpdate){
        ZyRepairNew zyRepairNew = new ZyRepairNew();
        String temp=LocalDateTime.now().toString();
        String replace = temp.replace("-", "");
        String t = replace.replace("T", "0");
        String replace1 = t.replace(":", "");
        String replace2 = replace1.replace(".", "");
        zyRepairNew.setOwnerId(repairUpdate.getOwnerId()).setCommunityId(repairUpdate.getCommunityId())
        .setDoorDate(repairUpdate.getDoorDate()).setDoorTime(repairUpdate.getDoorTime())
        .setAddress(repairUpdate.getAddress()).setPhoto(repairUpdate.getPhoto())
        .setRepairType(repairUpdate.getType()).setRepairContent(repairUpdate.getContext())
        .setRepairTime(LocalDateTime.now()).setRepairOrderNumber(replace2);
        repairService.save(zyRepairNew);
        return ResponseResult.ok("报修成功");
    }
    @GetMapping("/repairs")
    public ResponseResult getRepair(@RequestParam Long ownerId,@RequestParam Long repairType){

//        List<ZyRepair> list=null;
//        if(repairType==0||repairType==1||repairType==5){
//            list=repairService.getRepairs1(ownerId,repairType);
//        }else{
//            list = repairService.getRepairs(ownerId, repairType);
//        }
        List<ZyRepair> list = repairService.getRepairs(ownerId, repairType);
        return ResponseResult.ok(list);
    }
    @GetMapping("/repair-rate")
    public ResponseResult rate(@RequestParam Long repairId,@RequestParam Long rate){
        repairService.update(new UpdateWrapper<ZyRepairNew>().set("rate",rate).eq("repair_id",repairId));
        return ResponseResult.ok("评分成功");
    }
    @GetMapping("/repair-delete")
    public ResponseResult delete(@RequestParam Long repairId,@RequestParam Long rate){
        repairService.update(new UpdateWrapper<ZyRepairNew>().set("del_flag",1).eq("repair_id",repairId));
        return ResponseResult.ok("评分成功");
    }
}
